[contents] [prev] [next] [top] [bottom] (3 out of 7)

Multiple Inheritance

Multiple inheritance allows a subclass or instance to be created that inherits behavior from multiple superclasses. The new subclass or object inherits both class and instance variables and methods from each superclass, allowing you to combine the behavior of each superclass into a single subclass or object.

To create a new class or object that inherits from several superclasses, list the classes in the first line of the class or object definition, separated by commas:

class  variableName (class, class, class, . . . )
object [ variableName ] (class, class, class, . . . )
The newly created class inherits behavior and properties from all of its superclasses. This has the following effects:

Inheritance in multiply inherited ScriptX classes operates in a depth-first topological order. This means that all the superclasses of the first class in the list are searched before the superclasses of the next class in the inheritance list, and so on up the inheritance chain. To view this order , call getSupers on the new class.

You can control the inheritance of your subclass or object by controlling the order of its superclasses in the class or object definition. For example, if your class inherits from two classes, you can often simply reverse the order of those classes if the wrong method is being invoked first. Figure 6-1 shows a simple hierarchy of classes that illustrates the meaning of depth-first topological ordering.

Figure 6-1: An illustration of depth-first topological order.

Here are class definitions for Horse, Stallion, and Donkey, the parent classes of Mule:

class Horse () end
class Donkey () end
class Stallion (Horse) end
class Mule (Stallion, Donkey) end

If Mule is defined so that it inherits first from Stallion, then methods defined by both Stallion and Horse take precedence over methods defined by Donkey. To check the specific order in which ScriptX searches classes for a given generic function, you can use the getSupers generic function. getSupers returns an array of the superclasses of a given class in depth-first topological order, which is the order in which they are searched.

getSupers Mule
#(Stallion, Horse, Donkey, RootObject)

If Mule inherits first from Donkey, then methods defined by Donkey take precedence over methods defined by either Stallion or Horse. Notice how the ordering of classes reported by getSupers changes when we redefine the Mule class.

class Mule (Donkey, Stallion) end
getSupers Mule
#(Donkey, Stallion, Horse, RootObject)

We can use getSupers to determine the inheritance of any ScriptX class, no matter how complicated. Here are several examples drawn from the core classes.

class BouncyArray (Projectile, Array) end
getSupers BouncyArray
#(Projectile, Array, Sequence, LinearCollection, 
ImplicitlyKeyedCollection, Collection, RootObject)

class WeirdInheritance (QueuedEvent, TwoDShape, LinkedList) end
prin ((getSupers WeirdInheritance) as Array) @complete debug
#(QueuedEvent, Event, TwoDShape, TwoDPresenter, Presenter,
LinkedList, Sequence, LinearCollection, ImplicitlyKeyedCollection,
Collection, RootObject

The following example demonstrates the importance of inheritance order in classes and objects. The BassetHound and PitBull classes, subclasses of Dog, have their own implementations of the generic function getTemper.

class Dog () inst vars name end

-- create subclasses of Dog for the breeds BassetHound and PitBull
class BassetHound (Dog)
	instance methods
	method getTemper self -> (
		format debug "%*'s temper is good.\n" (self.name) @unadorned
	)
end

class PitBull (Dog)
instance methods
method getTemper self -> (
format debug "%*'s temper can be bad.\n" (self.name) @unadorned
)
end

Frookie and Noodle are both mutts. Noodle inherits first from BassetHound, so his temperament is usually good. Frookie inherits first from PitBull, so his temperament can be very bad.

object Frookie (PitBull, BassetHound) settings name:"Frookie" end

object Noodle (BassetHound, PitBull) settings name: "Noodle" end

-- test the getTemper method on both dogs 
getTemper Frookie
Frookie's temper can be bad.
getTemper Noodle
Noodle's temper is good.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.